package sdl2_impl

import (
	"code.google.com/p/go8/display"
	"github.com/gonutz/go-sdl2/sdl"
)

type Painter struct {
	renderer *sdl.Renderer
	window   *sdl.Window
}

func NewPainter(r *sdl.Renderer, w *sdl.Window) *Painter {
	return &Painter{r, w}
}

func (p *Painter) ClearScreen(c display.Color) {
	p.setRenderColor(c)
	p.renderer.Clear()
}

func (p *Painter) setRenderColor(c display.Color) {
	p.renderer.SetDrawColor(c.R, c.G, c.B, 255)
}

func (p *Painter) CanvasSize() (w, h int) {
	return p.window.GetSize()
}

func (p *Painter) FillRects(rects []display.Rect, c display.Color) {
	p.setRenderColor(c)
	p.renderer.FillRects(toSdlRects(rects))
}

func toSdlRects(rects []display.Rect) []sdl.Rect {
	result := make([]sdl.Rect, len(rects))
	for i, r := range rects {
		result[i] = sdl.Rect{int32(r.X), int32(r.Y), int32(r.W), int32(r.H)}
	}
	return result
}

func (p *Painter) Show() {
	p.renderer.Present()
}
